/* app02-window.c
 *
 * Copyright 2024 wan-iot
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "app02-config.h"
#include "app02-window.h"

struct _App02Window
{
  GtkApplicationWindow  parent_instance;

  /* Template widgets */
  GtkHeaderBar        *header_bar;
  GtkButton *btn_ok;
  GtkDialog *myDialog;
   GtkBox *dialog_box;
  GtkButtonBox *btn_box;
  GtkButton *btn_update;
  GtkButton *btn_reset;
  
  GtkEntry *id;
  GtkEntry *name;

};
GtkDialog *my_Dialog;
GtkEntry *id;
GtkEntry *name;
GtkButton *btn_update;
GtkButton *btn_reset;
G_DEFINE_TYPE (App02Window, app02_window, GTK_TYPE_APPLICATION_WINDOW)

static void
app02_window_class_init (App02WindowClass *klass)
{
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  gtk_widget_class_set_template_from_resource (widget_class, "/a/b/c/app02-window.ui");
  gtk_widget_class_bind_template_child (widget_class, App02Window, header_bar);

  gtk_widget_class_bind_template_child (widget_class, App02Window, btn_ok);
  gtk_widget_class_bind_template_child (widget_class, App02Window, myDialog);
  gtk_widget_class_bind_template_child (widget_class, App02Window, dialog_box);
  gtk_widget_class_bind_template_child (widget_class, App02Window, btn_box);
  gtk_widget_class_bind_template_child (widget_class, App02Window, btn_update);
  gtk_widget_class_bind_template_child (widget_class, App02Window, btn_reset);
  gtk_widget_class_bind_template_child (widget_class, App02Window, id);
  gtk_widget_class_bind_template_child (widget_class, App02Window, name);
}
void my_dialog_init(){
  //根据编号查询数据
  gtk_entry_set_text (id, "1001");
  //gtk_entry_set_overwrite_mode (id, FALSE);
  //gtk_entry_set_overwrite_mode (id, TRUE);
  //不可编辑
  gtk_editable_set_editable ((GtkEditable*)id, FALSE);
  gtk_entry_set_text (name, "admin");
}
void click_update(){
  g_print("id=%s\n",gtk_entry_get_text (id));
  g_print("name=%s\n",gtk_entry_get_text (name));
}
void click_reset(){
  //gtk_entry_set_text (name,"");
  //gtk_widget_destroy ((GtkWidget*)my_Dialog);
  gtk_widget_hide((GtkWidget*)my_Dialog);
}
void btn_open(){
  //GtkWidget *window =  g_object_new(MY_TYPE_DIALOG,NULL);
  //gtk_widget_show(window);
  g_print("clicked----\n");
  
  my_dialog_init();
  //打开修改的对话框
  gtk_widget_show((GtkWidget*)my_Dialog);
  
  g_signal_connect (btn_update, "clicked", G_CALLBACK(click_update), NULL);
  g_signal_connect (btn_reset, "clicked", G_CALLBACK(click_reset), NULL);
}
gboolean dialog_close(){
  g_print("close...\n");
  gtk_widget_hide((GtkWidget*)my_Dialog);

  return TRUE;
  
}
static void
app02_window_init (App02Window *self)
{
  GtkWidget * window = GTK_WIDGET (self);

  gtk_widget_init_template (window);
  my_Dialog = self->myDialog;
  id = self->id;
  name = self->name;
  btn_update = self->btn_update;
  btn_reset = self->btn_reset;
  g_signal_connect (self->btn_ok, "clicked", G_CALLBACK(btn_open), NULL);
  
  g_signal_connect (my_Dialog, "delete_event", G_CALLBACK(dialog_close), NULL);
  
}
